<html>

<head>
    <meta charset="utf-8">
    <title>添加商品</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/shop/static/layui/css/layui.css" media="all" type="text/css">
    <link rel="stylesheet" href="/shop/static/css/public.css" media="all" type="text/css"> </head>

<body class="childrenBody">
    <form id="actionForm" class="layui-form" lay-filter="example">
        <input type="hidden" name="id" id="id" value="${product.id!'0'}">
        <input type="hidden" name="productCover" id="productCover" value="${product.productCover!'/shop/static/images/userface4.jpg'}">
        <div class="layui-col-md6 layui-col-xs12">
            <div class="layui-form-item">
                <label class="layui-form-label">产品名称</label>
                <div class="layui-input-block">
                    <input type="text" name="productName" lay-verify="required" placeholder="请输入商品名称" class="layui-input realName" value="${product.productName!''}"> </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">封面</label>
                <div class="layui-input-block"> 
                	<a class="img" title="缩略图" ><img id="upload" class="thumbBox mag0 magt3" alt="点击上传" src="${product.productCover!'/shop/static/images/userface4.jpg'}"></a>
                    <input id="upfile" style="display: none" type="file" name="upfile"> 
                </div>
            </div>
            <div class="layui-form-item">
				<label class="layui-form-label">所属类别</label>
				<div class="layui-input-block">
					<select name="productType" class="layui-input"  >
                          ${FreemarkUtil.getOption("productType","")}
					</select>
				</div>
            </div>
            <div class="layui-form-item">
				<label class="layui-form-label">是否上架</label>
				<div class="layui-input-block">
					<select name="publisherState" class="layui-input"  >
                          ${FreemarkUtil.getOption("yesOrNo","")}
					</select>
				</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上架价格</label>
                <div class="layui-input-block">
                    <input type="text" name="productPrice" lay-verify="required|number" placeholder="请输入价格" class="layui-input realName" value="${product.productPrice!''}" > </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上架数量</label>
                <div class="layui-input-block">
                    <input type="text" name="productNumber" lay-verify="required|number"  placeholder="请输入数量" class="layui-input realName" value="${product.productNumber!''}" > </div>
            </div>
         </div>
         <div class="layui-col-md10 layui-col-xs12">
            <div class="layui-form-item">
                <label class="layui-form-label">产品简介</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="productSynopsis" lay-verify="required" name="productSynopsis" class="layui-textarea myself">${product.productSynopsis!''}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" type="button" lay-filter="form">立即提交</button>
                </div>
            </div>
         </div>
    </form>
    <script src="/shop/static/layui/layui.js" charset="utf-8" type="text/javascript"></script>
    <script type="text/javascript" src="/shop/static/js/common/jquery-2.1.0.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="/shop/static/js/common/ajaxfileupload.js"></script>    
    <script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate'], function() {
        var form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            layedit = layui.layedit;
        layedit.set({
        	  uploadImage: {
        	    url: '/shop/upload/layedit'
        	  }
        	});
        layedit.build('productSynopsis'); //建立编辑器
        form.on('submit(form)', function() {
                $.ajax({
                    cache: true,
                    type: "POST",
                    url: "/shop/shops/saveUpProduct",
                    data: $('#actionForm').serialize(),
                    async: false,
                    error: function(request) {
                        layer.msg('系统错误！');
                    },
                    success: function(data) {
                    	
                        if(data.code == "1") {
                            layer.msg(data.msg, {
                                time: 1000
                            }, function() { //提示框设定显示1秒
                            	window.location.href="/shop/shops/productList"; 
                            })
                        } else {
                            layer.msg(data.msg);
                        }
                    }
                });
            });
        //表单初始赋值
        form.val('example', {
            "productType": "${product.productType!''}",
            "publisherState": "${product.publisherState!''}"
        })
    });
    /* 上传图片 */
    $("#upload").on('click', function() {
        $("#upfile").click();
    });
    $("#upfile").on("change", upfile);

    function upfile() {
        var fileSize = $("#upfile")[0].files[0].size;//文件的大小，单位为字节B
        if(fileSize > 1048576) {
        	layui.use('layer', function(){
        		  var layer = layui.layer;
        		  layer.msg('请上传大小小于1M的图片');
        		});
        	 
        }else{
            $.ajaxFileUpload({
                secureuri: false,
                url: "/shop/upload/springUpload",
                dataType: "json",
                //fileElementId是img标签的id  
                fileElementId: 'upfile',
                success: function(data) {
                    //上传成功后改变<img>标签的src地址，id参数其实不需要，主要是为了刷新img的图片资源。  
                    $("#upload").attr("src", data.path);
                    $("#productCover").val(data.path);
                    //重新绑定事件，防止二次上传时失效  
                    $("#upfile").on("change", upfile);
                }
            });        	
        }
    }
    </script>
</body>

</html>